package org.example.lethear.repository;

import org.example.lethear.entity.Follow;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 关注关系数据访问接口
 */
@Repository
public interface FollowRepository extends JpaRepository<Follow, Long> {
    
    /**
     * 根据关注者和被关注者查找关注关系
     */
    Optional<Follow> findByFollowerIdAndFollowingId(Long followerId, Long followingId);
    
    /**
     * 检查是否存在关注关系
     */
    boolean existsByFollowerIdAndFollowingId(Long followerId, Long followingId);
    
    /**
     * 查找用户关注的人
     */
    List<Follow> findByFollowerId(Long followerId);
    
    /**
     * 分页查找用户关注的人
     */
    Page<Follow> findByFollowerId(Long followerId, Pageable pageable);
    
    /**
     * 查找用户的粉丝
     */
    List<Follow> findByFollowingId(Long followingId);
    
    /**
     * 分页查找用户的粉丝
     */
    Page<Follow> findByFollowingId(Long followingId, Pageable pageable);
    
    /**
     * 统计用户关注的人数
     */
    @Query("SELECT COUNT(f) FROM Follow f WHERE f.followerId = :followerId")
    long countFollowing(@Param("followerId") Long followerId);
    
    /**
     * 统计用户的粉丝数
     */
    @Query("SELECT COUNT(f) FROM Follow f WHERE f.followingId = :followingId")
    long countFollowers(@Param("followingId") Long followingId);
    
    /**
     * 查找互相关注的用户
     */
    @Query("SELECT f1 FROM Follow f1 WHERE EXISTS (SELECT f2 FROM Follow f2 WHERE f1.followerId = f2.followingId AND f1.followingId = f2.followerId)")
    List<Follow> findMutualFollows();
    
    /**
     * 检查两个用户是否互相关注
     */
    @Query("SELECT CASE WHEN COUNT(f) = 2 THEN true ELSE false END FROM Follow f WHERE (f.followerId = :userId1 AND f.followingId = :userId2) OR (f.followerId = :userId2 AND f.followingId = :userId1)")
    boolean areMutuallyFollowing(@Param("userId1") Long userId1, @Param("userId2") Long userId2);
    
    /**
     * 查找最新关注记录
     */
    @Query("SELECT f FROM Follow f ORDER BY f.createTime DESC")
    Page<Follow> findRecentFollows(Pageable pageable);
    
    /**
     * 查找指定时间段内的关注记录
     */
    @Query("SELECT f FROM Follow f WHERE f.createTime BETWEEN :startTime AND :endTime")
    List<Follow> findFollowsByCreateTimeBetween(@Param("startTime") LocalDateTime startTime, 
                                               @Param("endTime") LocalDateTime endTime);
    
    /**
     * 统计今日新增关注数
     */
    @Query("SELECT COUNT(f) FROM Follow f WHERE f.createTime >= :startOfDay")
    long countTodayFollows(@Param("startOfDay") LocalDateTime startOfDay);
    
    /**
     * 查找热门用户（粉丝数最多）
     */
    @Query("SELECT f.followingId, COUNT(f) as followerCount FROM Follow f GROUP BY f.followingId ORDER BY followerCount DESC")
    Page<Object[]> findPopularUsers(Pageable pageable);
    
    /**
     * 查找活跃用户（关注人数最多）
     */
    @Query("SELECT f.followerId, COUNT(f) as followingCount FROM Follow f GROUP BY f.followerId ORDER BY followingCount DESC")
    Page<Object[]> findActiveUsers(Pageable pageable);
    
    /**
     * 删除关注关系
     */
    void deleteByFollowerIdAndFollowingId(Long followerId, Long followingId);
    
    /**
     * 删除用户的所有关注关系（用户注销时）
     */
    void deleteByFollowerIdOrFollowingId(Long userId, Long userId2);
    
    /**
     * 查找用户可能认识的人（朋友的朋友）
     */
    @Query("SELECT DISTINCT f2.followingId FROM Follow f1 JOIN Follow f2 ON f1.followingId = f2.followerId WHERE f1.followerId = :userId AND f2.followingId != :userId AND NOT EXISTS (SELECT f3 FROM Follow f3 WHERE f3.followerId = :userId AND f3.followingId = f2.followingId)")
    List<Long> findSuggestedUsers(@Param("userId") Long userId, Pageable pageable);
} 